//
//  QGRegistByEmailViewController.m
//  QuanGou
//
//  Created by qqqq on 15/11/21.
//  Copyright © 2015年 DongYongFei. All rights reserved.
//

#import "QGRegistByEmailViewController.h"
#import <PhotoTweaksViewController.h>

@interface QGRegistByEmailViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,PhotoTweaksViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;

@end

@implementation QGRegistByEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.photoImageView.layer.cornerRadius = 50;
    self.photoImageView.userInteractionEnabled = YES;
    self.photoImageView.clipsToBounds = YES;
    self.photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    UITapGestureRecognizer *tapPhoto = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGuestureClick:)];
    [self.photoImageView addGestureRecognizer:tapPhoto];
    UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewClick:)];
    [self.view addGestureRecognizer:tapView];

}

- (IBAction)regist:(id)sender {
    
    AVUser *user = [AVUser user];
    user.username = @"未设置";
    user.password = self.passwordTextField.text;
    user.email = self.emailTextField.text;
    
    AVObject *userPost = [AVObject objectWithClassName:@"UserModel"];
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            if (self.photoImageView.image != nil) {
                NSData *imageData = UIImagePNGRepresentation(self.photoImageView.image);
                AVFile *imageFile = [AVFile fileWithName:[NSString stringWithFormat:@"%@.png", user.objectId] data:imageData];
                [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (!error) {
                        NSLog(@"2");
                        [userPost setObject:user.objectId forKey:@"userId"];
                        userPost[@"sex"] = @"暂无";
                        [userPost setObject:imageFile forKey:@"userImage"];
                        [userPost save];
                        NSLog(@"注册成功");
                    }
                }];
            }

        }else {
            NSLog(@"注册失败");
        }
    }];
}

- (void)tapViewClick:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}
- (void)photoTweaksController:(PhotoTweaksViewController *)controller didFinishWithCroppedImage:(UIImage *)croppedImage{
    
    self.photoImageView.image = croppedImage;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoTweaksControllerDidCancel:(PhotoTweaksViewController *)controller {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)tapGuestureClick:(UITapGestureRecognizer *)tap {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *phontoListAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        
        [self showDetailViewController:imagePicker sender:nil];
        
    }];
    UIAlertAction *camereAction = [UIAlertAction actionWithTitle:@"从相机拍照选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        
        [self showDetailViewController:imagePicker sender:nil];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    
    [alertController addAction:phontoListAction];
    [alertController addAction:camereAction];
    [alertController addAction:cancleAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    PhotoTweaksViewController *photoTweaksViewController = [[PhotoTweaksViewController alloc] initWithImage:image];
    photoTweaksViewController.delegate = self;
    [picker presentViewController:photoTweaksViewController animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
